package com.example.studykotlin.kotlin_actual_combat.art.aidl.custom;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;

import com.example.studykotlin.kotlin_actual_combat.art.aidl.Book;
import com.example.studykotlin.kotlin_actual_combat.art.aidl.IOnNewBookArrivedListener;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class MyBookManagerService extends Service {
    private static final String TAG = "MyBookManagerService";

    private AtomicBoolean mIsServiceDestroyed = new AtomicBoolean(false);

    public MyBookManagerService() {
    }

    private CopyOnWriteArrayList<Book> mBookList = new CopyOnWriteArrayList<>();
    RemoteCallbackList<IOnNewBookArrivedListener> mListenerList = new RemoteCallbackList<>();

    private Binder mBinder = new MyBookManagerImpl() {
        @Override
        public List<Book> getBookList() throws RemoteException {
            return mBookList;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            mBookList.add(book);
        }

        @Override
        public void registerListener(IOnNewBookArrivedListener listener) throws RemoteException {
            mListenerList.register(listener);
            final int N = mListenerList.beginBroadcast();
            mListenerList.finishBroadcast();
            Log.e(TAG, "registerListener, current size:" + N);
        }

        @Override
        public void unregisterListener(IOnNewBookArrivedListener listener) throws RemoteException {
            mListenerList.unregister(listener);
            final int N = mListenerList.beginBroadcast();
            mListenerList.finishBroadcast();
            Log.e(TAG, "unregisterListener, current size:" + N);
        }
    };



    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBookList.add(new Book(1, "Android"));
        mBookList.add(new Book(1, "ios"));
        new Thread(new ServiceWork()).start();
    }

    @Override
    public void onDestroy() {
        mIsServiceDestroyed.set(true);
        super.onDestroy();
    }

    private class ServiceWork implements Runnable {
        @Override
        public void run() {
            while (!mIsServiceDestroyed.get()) {
                try {
                    Thread.sleep(3000);
                    int bookId = mBookList.size() + 1;
                    Book newBook = new Book(bookId, "book#" + bookId);
                    onNewBookArrived(newBook);
                } catch (InterruptedException | RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void onNewBookArrived(Book book) throws RemoteException {
        mBookList.add(book);
        int size = mListenerList.beginBroadcast();
        Log.e(TAG, "onNewBookArrived notify listener:"+size );
        for (int i = 0; i < size; i++) {
            IOnNewBookArrivedListener listener = mListenerList.getBroadcastItem(i);
            if (listener != null) {
                listener.onNewBookArrived(book);
            }
        }

        mListenerList.finishBroadcast();
    }
}
